﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using QASPRibbon.Commands;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a ColorPicker control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonColorPicker : RibbonButton
    {
        #region JavaScript

        /// <summary>
        /// Client-side script that executes when mouse pointer on color hover.
        /// </summary>
        public string OnClientColorPreview { get; set; }

        /// <summary>
        /// Client-side script that executes when menu closed without color selection.
        /// </summary>
        public string OnClientColorRevert { get; set; }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a collection of colors.
        /// </summary>
        public IEnumerable<RibbonColor> Colors { get; set; }

        #endregion

        #region Internal

        internal override string XmlTag
        {
            get { return Schema.ColorPicker; }
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute(Schema.CommandPreview, FullId + Schema.CommandPreview));
            element.Add(new XAttribute(Schema.CommandRevert, FullId + Schema.CommandRevert));
            var colorsElement = new XElement(Schema.Colors);
            foreach (var color in Colors)
            {
                color.NameSpace = FullId;
                colorsElement.Add(color.AsXElement());
            }
            element.Add(colorsElement);
            return element;
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>
                              {
                                  new RibbonCommand(FullId, Schema.Command, string.Format("{0}; {1}", OnClientClick ?? string.Empty, (AllowPostBack
                                                                       ? string.Format("__doPostBack('{0}', properties.Color+'|'+properties.Style);", FullId)
                                                                       : string.Empty))),
                                  new RibbonCommand(FullId, Schema.CheckState, string.Format("{0}; {1}; return {2};", OnClientActivate ?? string.Empty, OnClientStateCheck, Enabled ? "true" : "false"))
                              };
                if (!string.IsNullOrEmpty(OnClientColorPreview))
                    res.Add(new RibbonCommand(FullId, Schema.CommandPreview, OnClientColorPreview));
                if (!string.IsNullOrEmpty(OnClientColorRevert))
                    res.Add(new RibbonCommand(FullId, Schema.CommandRevert, OnClientColorRevert));
                return res;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        public RibbonColorPicker() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public RibbonColorPicker(string id) : base(id) { }

        #endregion

        #region Private

        #endregion

        #region PostBack Action

        /// <summary>
        /// 
        /// </summary>
        public new event RibbonColorPikcerEventHandler OnAction;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public void DoOnAction(RibbonColorPickerEventArgs e)
        {
            var handler = OnAction;
            if (handler != null) handler(this, e);
        }

        #endregion

        #region StandardColors

        /// <summary>
        /// 
        /// </summary>
        public static IEnumerable<RibbonColor> StandardColors
        {
            get
            {
                return new[]
                           {
                               new RibbonColor
                                   {Title = "Light 1", Style = "ms-rteThemeBackColor-1-0", DisplayColor = "#ffffff"},
                               new RibbonColor
                                   {Title = "Dark 1", Style = "ms-rteThemeBackColor-2-0", DisplayColor = "#000000"},
                               new RibbonColor
                                   {Title = "Light 2", Style = "ms-rteThemeBackColor-3-0", DisplayColor = "#e9f5fa"},
                               new RibbonColor
                                   {Title = "Dark 2", Style = "ms-rteThemeBackColor-4-0", DisplayColor = "#33557b"},
                               new RibbonColor
                                   {Title = "Accent 1", Style = "ms-rteThemeBackColor-5-0", DisplayColor = "#00adee"},
                               new RibbonColor
                                   {Title = "Accent 2", Style = "ms-rteThemeBackColor-6-0", DisplayColor = "#ec008c"},
                               new RibbonColor
                                   {Title = "Accent 3", Style = "ms-rteThemeBackColor-7-0", DisplayColor = "#1d71b6"},
                               new RibbonColor
                                   {Title = "Accent 4", Style = "ms-rteThemeBackColor-8-0", DisplayColor = "#fd9f08"},
                               new RibbonColor
                                   {Title = "Accent 5", Style = "ms-rteThemeBackColor-9-0", DisplayColor = "#62ce33"},
                               new RibbonColor
                                   {Title = "Accent 6", Style = "ms-rteThemeBackColor-10-0", DisplayColor = "#fae032"},
                               new RibbonColor
                                   {Title = "Light 1 Lightest", Style = "ms-rteThemeBackColor-1-1", DisplayColor = "#f2f2f2"},
                               new RibbonColor
                                   {
                                       Title = "Dark 1 Lightest",
                                       Style = "ms-rteThemeBackColor-2-1",
                                       DisplayColor = "#7f7f7f"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 2 Lightest",
                                       Style = "ms-rteThemeBackColor-3-1",
                                       DisplayColor = "#c1e3f1"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 2 Lightest",
                                       Style = "ms-rteThemeBackColor-4-1",
                                       DisplayColor = "#cfddeb"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 1 Lightest",
                                       Style = "ms-rteThemeBackColor-5-1",
                                       DisplayColor = "#c8f0ff"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 2 Lightest",
                                       Style = "ms-rteThemeBackColor-6-1",
                                       DisplayColor = "#ffc8e8"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 3 Lightest",
                                       Style = "ms-rteThemeBackColor-7-1",
                                       DisplayColor = "#cbe3f6"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 4 Lightest",
                                       Style = "ms-rteThemeBackColor-8-1",
                                       DisplayColor = "#feebcd"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 5 Lightest",
                                       Style = "ms-rteThemeBackColor-9-1",
                                       DisplayColor = "#e0f5d6"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 6 Lightest",
                                       Style = "ms-rteThemeBackColor-10-1",
                                       DisplayColor = "#fef8d6"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 1 Lighter",
                                       Style = "ms-rteThemeBackColor-1-2",
                                       DisplayColor = "#d8d8d8"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 1 Lighter",
                                       Style = "ms-rteThemeBackColor-2-2",
                                       DisplayColor = "#595959"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 2 Lighter",
                                       Style = "ms-rteThemeBackColor-3-2",
                                       DisplayColor = "#86c8e3"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 2 Lighter",
                                       Style = "ms-rteThemeBackColor-4-2",
                                       DisplayColor = "#a0bad8"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 1 Lighter",
                                       Style = "ms-rteThemeBackColor-5-2",
                                       DisplayColor = "#92e1ff"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 2 Lighter",
                                       Style = "ms-rteThemeBackColor-6-2",
                                       DisplayColor = "#ff91d2"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 3 Lighter",
                                       Style = "ms-rteThemeBackColor-7-2",
                                       DisplayColor = "#97c7ee"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 4 Lighter",
                                       Style = "ms-rteThemeBackColor-8-2",
                                       DisplayColor = "#fed89c"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 5 Lighter",
                                       Style = "ms-rteThemeBackColor-9-2",
                                       DisplayColor = "#c0ebad"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 6 Lighter",
                                       Style = "ms-rteThemeBackColor-10-2",
                                       DisplayColor = "#fef8d6"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 1 Medium",
                                       Style = "ms-rteThemeBackColor-1-3",
                                       DisplayColor = "#bfbfbf"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 1 Medium",
                                       Style = "ms-rteThemeBackColor-2-3",
                                       DisplayColor = "#3f3f3f"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 2 Medium",
                                       Style = "ms-rteThemeBackColor-3-3",
                                       DisplayColor = "#2c98c4"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 2 Medium",
                                       Style = "ms-rteThemeBackColor-4-3",
                                       DisplayColor = "#7098c4"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 1 Medium",
                                       Style = "ms-rteThemeBackColor-5-3",
                                       DisplayColor = "#5bd2ff"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 2 Medium",
                                       Style = "ms-rteThemeBackColor-6-3",
                                       DisplayColor = "#ff5abc"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 3 Medium",
                                       Style = "ms-rteThemeBackColor-7-3",
                                       DisplayColor = "#64abe6"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 4 Medium",
                                       Style = "ms-rteThemeBackColor-8-3",
                                       DisplayColor = "#fdc56a"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 5 Medium",
                                       Style = "ms-rteThemeBackColor-9-3",
                                       DisplayColor = "#b9de8c"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 6 Medium",
                                       Style = "ms-rteThemeBackColor-10-3",
                                       DisplayColor = "#fcec84"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 1 Darker",
                                       Style = "ms-rteThemeBackColor-1-4",
                                       DisplayColor = "#a5a5a5"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 1 Darker",
                                       Style = "ms-rteThemeBackColor-2-4",
                                       DisplayColor = "#262626"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 2 Darker",
                                       Style = "ms-rteThemeBackColor-3-4",
                                       DisplayColor = "#164c62"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 2 Darker",
                                       Style = "ms-rteThemeBackColor-4-4",
                                       DisplayColor = "#26405c"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 1 Darker",
                                       Style = "ms-rteThemeBackColor-5-4",
                                       DisplayColor = "#0081b2"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 2 Darker",
                                       Style = "ms-rteThemeBackColor-6-4",
                                       DisplayColor = "#b10069"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 3 Darker",
                                       Style = "ms-rteThemeBackColor-7-4",
                                       DisplayColor = "#155488"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 4 Darker",
                                       Style = "ms-rteThemeBackColor-8-4",
                                       DisplayColor = "#c27801"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 5 Darker",
                                       Style = "ms-rteThemeBackColor-9-4",
                                       DisplayColor = "#499b25"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 6 Darker",
                                       Style = "ms-rteThemeBackColor-10-4",
                                       DisplayColor = "#dbbf05"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 1 Darkest",
                                       Style = "ms-rteThemeBackColor-1-5",
                                       DisplayColor = "#7f7f7f"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 1 Darkest",
                                       Style = "ms-rteThemeBackColor-2-5",
                                       DisplayColor = "#0c0c0c"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Light 2 Darkest",
                                       Style = "ms-rteThemeBackColor-3-5",
                                       DisplayColor = "#081e27"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Dark 2 Darkest",
                                       Style = "ms-rteThemeBackColor-4-5",
                                       DisplayColor = "#192a3e"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 1 Darkest",
                                       Style = "ms-rteThemeBackColor-5-5",
                                       DisplayColor = "#005677"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 2 Darkest",
                                       Style = "ms-rteThemeBackColor-6-5",
                                       DisplayColor = "#760046"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 3 Darkest",
                                       Style = "ms-rteThemeBackColor-7-5",
                                       DisplayColor = "#0e385b"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 4 Darkest",
                                       Style = "ms-rteThemeBackColor-8-5",
                                       DisplayColor = "#815001"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 5 Darkest",
                                       Style = "ms-rteThemeBackColor-9-5",
                                       DisplayColor = "#316819"
                                   },
                               new RibbonColor
                                   {
                                       Title = "Accent 6 Darkest",
                                       Style = "ms-rteThemeBackColor-10-5",
                                       DisplayColor = "#927f03"
                                   }
                           };
            }
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void RibbonColorPikcerEventHandler(object sender, RibbonColorPickerEventArgs e);

    /// <summary>
    /// 
    /// </summary>
    public class RibbonColorPickerEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        public RibbonColorPicker Instance;
        /// <summary>
        /// 
        /// </summary>
        public string Style;
        /// <summary>
        /// 
        /// </summary>
        public string Color;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="button"></param>
        /// <param name="args"></param>
        public RibbonColorPickerEventArgs(RibbonColorPicker button, string args)
        {
            Instance = button;
            var arr = args.Split('|');
            if (arr.Length > 0)
            {
                Color = arr[0];
                if (arr.Length > 1)
                {
                    Style = arr[1];
                }
            }
        }
    }
}
